package com.psc.kpjy.view.customized;

import android.content.Context;
import android.content.res.Resources;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.util.AttributeSet;
import android.view.View;

import androidx.annotation.Nullable;

import com.psc.kpjy.R;
import com.psc.kpjy.view.utils.DpPxUtils;

/**
 * Creator:Yx
 * Time:2020/7/21 11:45
 * Description:年标签，年份，年代
 */
public class YearLabelView extends View {

    // 线画笔
    private Paint linePaint;
    // 点画笔
    private Paint dotPaint;
    // 年文字画笔
    private Paint textPaint;
    // 圆点半径
    private int dotRadius = dpToPx(5);
    // 线的颜色
    private int lineColor = Color.parseColor("#FF4621");
    // 点的颜色
    private int dotColor = Color.parseColor("#FF4621");
    // 文字颜色
    private int textColor = Color.parseColor("#FF4621");
    // View 的宽
    private float viewWidth;
    // View 的高
    private float viewHeight;

    // 右边视图绘制起始点
    private float startX;
    private float startY;

    private boolean mDirection = true; // false左，true右

    private String textStr = "1026";


    public YearLabelView(Context context) {
        super(context);
        initPaint(context, null);
    }

    public YearLabelView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        initPaint(context, attrs);
    }

    public YearLabelView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initPaint(context, attrs);
    }

    public YearLabelView(Context context, @Nullable AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
        initPaint(context, attrs); // 初始画笔
    }

    /**
     * 初始内容
     */
    private void initPaint(Context context, AttributeSet attrs) {
        //  读取配置
        TypedArray array = context.obtainStyledAttributes(attrs, R.styleable.YearLabelView);
        mDirection = array.getBoolean(R.styleable.YearLabelView_mDirection, true);
        lineColor = array.getColor(R.styleable.YearLabelView_mColor, Color.WHITE);
        dotColor = array.getColor(R.styleable.YearLabelView_mColor, Color.WHITE);
        textColor = array.getColor(R.styleable.YearLabelView_mColor, Color.WHITE);
        array.recycle();
        // 线画笔
        linePaint = new Paint();
        linePaint.setColor(lineColor);
        linePaint.setAntiAlias(true);
        linePaint.setStrokeWidth(2);
        linePaint.setStyle(Paint.Style.FILL_AND_STROKE);
        // 点画笔
        dotPaint = new Paint();
        dotPaint.setColor(dotColor);
        dotPaint.setAntiAlias(true);
        dotPaint.setStyle(Paint.Style.FILL);
        // 年文字画笔
        textPaint = new Paint();
        textPaint.setColor(textColor);
        textPaint.setTextAlign(Paint.Align.LEFT); // 左对齐
        textPaint.setStyle(Paint.Style.FILL_AND_STROKE);
        textPaint.setTextSize(DpPxUtils.dip2px(16));
        textPaint.setFakeBoldText(true); // 加粗
        textPaint.setStrokeWidth(2);
        textPaint.setAntiAlias(true);
    }


    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        viewWidth = w;
        viewHeight = h;
        startX = dotRadius;
        startY = viewHeight / 2;
        // 一旦size发生改变，重新绘制
        postInvalidate();
        super.onSizeChanged(w, h, oldw, oldh);
    }

    /**
     * 动态设置数据，年份，颜色等
     */
    public void setDatas(int colors, String textStr) {
        lineColor = colors;
        linePaint.setColor(lineColor);
        dotColor = colors;
        dotPaint.setColor(colors);
        textColor = colors;
        textPaint.setColor(colors);
        this.textStr = textStr;
        postInvalidate();

    }

    @Override
    protected void onDraw(Canvas canvas) {
        if (mDirection) {
            drawRight(canvas);
        } else {
            drawLeft(canvas);
        }
    }


    /**
     * 右边视图
     *
     * @param canvas
     */
    private void drawRight(Canvas canvas) {
        // 点
        canvas.drawCircle(startX, startY, dotRadius, dotPaint);
        // 线1
        Path path = new Path();
        path.reset();
        path.moveTo(startX, startY);
        float toX = (float) (startX * 2 + dpToPx(15)); // 线1结束点
        float toY = startY;
        path.lineTo(toX, toY);
        canvas.drawPath(path, linePaint);
        // 文字年份
        Paint.FontMetrics fontMetrics = textPaint.getFontMetrics();
        float fontHeight = fontMetrics.descent - fontMetrics.ascent; // 文字高度，标题高度
        float strX = toX - dpToPx(5); // 文字起始点
        float strY = startY + 1f; // 文字Y轴基准线
        if (isNumeric(textStr)) {
            // 是数字
            canvas.drawText(textStr, strX, strY, textPaint);
        } else {
            // 是字符串
            textStr = "0000";
            float dis = textPaint.measureText(textStr); // 文字区域总体长度
            canvas.drawLine(strX, startY, strX + dis, startY, linePaint);
        }
        // 线2
        // float dis = textPaint.measureText(textStr); // 文字区域总体长度
        float dis = getTextWidth(textPaint, textStr); // 文字精确长度
        float startX2 = toX + dis - dpToPx(8); // 线2起始点
        path.moveTo(startX2, startY);
        float toX2 = (float) (startX2 + dpToPx(15)); // 线2结束点
        float toY2 = startY;
        path.lineTo(toX2, toY2);
        canvas.drawPath(path, linePaint);
        // 线3斜线
        float startX3 = toX2; // 线3起始点
        float toX3 = (float) (startX3 + dpToPx(3) - 1); // 线3结束点X  。这个+dp是斜线长度 -1是微调
        float startY3 = startY;
        float toY3 = startY - dpToPx(4); // 线3结束点Y  。这个-dp是斜线高度
        canvas.drawLine(startX3, startY3, toX3, toY3, linePaint);
        // 线4
        path.moveTo(toX3, toY3); // 线4起始点
        // float toX4 = (float) (viewWidth - dpToPx(20)); // 线4结束点
        float toX4 = (float) (viewWidth - dpToPx(5)); // 线4结束点
        float toY4 = toY3;
        path.lineTo(toX4, toY4);
        canvas.drawPath(path, linePaint);

        path.close();
    }


    /**
     * 左边视图
     *
     * @param canvas
     */
    private void drawLeft(Canvas canvas) {
        float leftStartX = viewWidth - dotRadius;
        // 点
        canvas.drawCircle(leftStartX, startY, dotRadius, dotPaint);
        // 线1
        Path path = new Path();
        path.reset();
        path.moveTo(leftStartX, startY);
        float toX = (float) (leftStartX - dotRadius - dpToPx(15)); // 线1结束点
        float toY = startY;
        path.lineTo(toX, toY);
        canvas.drawPath(path, linePaint);
        // 文字年份
        float dis = getTextWidth(textPaint, textStr); // 文字长度
        float strX = toX - dis + dpToPx(5); // 文字起始点
        if (isNumeric(textStr)) {
            // 是数字
            float strY = startY + 1f; // 文字Y轴基准线
            canvas.drawText(textStr, strX, strY, textPaint);
        } else {
            // 是字符串
            textStr = "0000";
            dis = getTextWidth(textPaint, textStr); // 文字长度
            strX = toX - dis + dpToPx(5); // 文字起始点
            canvas.drawLine(strX, startY, strX + dis, startY, linePaint);
        }
        // 线2
        path = new Path();
        path.reset();
        // float dis = textPaint.measureText(textStr); // 文字区域总体长度
        float startX2 = strX + dpToPx(5); // 线2起始点
        path.moveTo(startX2, startY);
        float toX2 = (float) (startX2 - dpToPx(18)); // 线2结束点
        float toY2 = startY;
        path.lineTo(toX2, toY2);
        canvas.drawPath(path, linePaint);
        // 线3斜线
        path = new Path();
        path.reset();
        float startX3 = toX2; // 线3起始点
        float toX3 = (float) (startX3 - dpToPx(3) + 1); // 线3结束点X  。这个-dp是斜线长度 +1是微调
        float startY3 = startY;
        float toY3 = startY - dpToPx(4); // 线3结束点Y  。这个-dp是斜线高度
        canvas.drawLine(startX3, startY3, toX3, toY3, linePaint);
        // 线4
        path = new Path();
        path.reset();
        path.moveTo(toX3, toY3); // 线4起始点
        // float toX4 = (float) (dpToPx(19)); // 线4结束点
        float toX4 = (float) (dpToPx(5)); // 线4结束点
        float toY4 = toY3;
        path.lineTo(toX4, toY4);
        canvas.drawPath(path, linePaint);

        path.close();
    }


    public int dpToPx(int dp) {
        float scale = Resources.getSystem().getDisplayMetrics().density;
        return (int) (dp * scale + 0.5f);
    }

    // 文字宽度精确
    private int getTextWidth(Paint paint, String str) {
        int iRet = 0;
        if (str != null && str.length() > 0) {
            int len = str.length();
            float[] widths = new float[len];
            paint.getTextWidths(str, widths);
            for (int j = 0; j < len; j++) {
                iRet += (int) Math.ceil(widths[j]);
            }
        }
        return iRet;
    }

    // 是否是数字
    public boolean isNumeric(String str) {
        for (int i = str.length(); --i >= 0; ) {
            if (!Character.isDigit(str.charAt(i))) {
                return false;
            }
        }
        return true;
    }
}
